package com.数据结构.线性表;

import java.util.*;

/**
 * @Date 2021/4/8 10:42
 * @Author WangLinBao
 * @Description
 */
public class ArrayList<E> implements List<E> {
    //默认容量
    public static final int DEFAULT_CAPACITY = 10;
    private int size;
    //数组
    private E[] data;

    //构造器
    public ArrayList(int capacity){
        data = (E[]) new Object[capacity];
        size = 0;
    }
    public ArrayList(){
        this(DEFAULT_CAPACITY);
    }


    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public boolean contains(Object o) {
        for(int i = 0;i<size;i++){
            if(data[i].equals(o)){
                return true;
            }
        }
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return null;
    }

    @Override
    public boolean add(E e) {
        if(size==data.length){
            //扩容
            int newCapacity = data.length+1<<1;
            grow(newCapacity);
        }
        data[size++] = e;
        return true;
    }

    @Override
    public boolean remove(Object o) {
        for (int i = 0; i < size; i++) {
            if(o==null){
                if(data[i]==null){
                    remove(i);
                    return true;
                }

            }else{
                if(o.equals(data[i])){
                    remove(i);
                    return true;
                }
            }
        }
        return false;
    }

    private void grow(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        System.arraycopy(data,0,newData,0,size);
        data = newData;
    }


    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public E get(int index) {
        if(index<0 || index>=size){
            throw new ArrayIndexOutOfBoundsException("越界");
        }
        return data[index];
    }

    @Override
    public E set(int index, E element) {
        if(index<0 || index>=size){
            throw new ArrayIndexOutOfBoundsException("越界");
        }
        E val = data[index];
        data[index] = element;
        return val;
    }

    @Override
    public void add(int index, E element) {
        if(index<0 || index>size){
            throw new IndexOutOfBoundsException("IndexOutOfBoundsException");
        }
        if(size==data.length){
            grow(size+1<<1);
        }
        for(int i = size-1;i>=index;i--){
            data[i+1] = data[i];
        }
        data[index] = element;
        size++;
    }

    @Override
    public E remove(int index) {
        if(index<0 || index>=size){
            throw new IndexOutOfBoundsException("IndexOutOfBoundsException");
        }
        E datum = data[index];
        for (int i = index; i < size-1; i++) {
            data[i] = data[i+1];
            data[size-1] = null;
        }
        size--;
        //反扩容
        if(data.length>>1 > size){
            grow(data.length>>1);
        }
        return datum;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }


    @Override
    public ListIterator<E> listIterator() {
        return null;
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        return null;
    }

    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        return null;
    }
}
